package servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Hashtable;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import ejbSessions.ManagerAbilitaRemote;
import servizio.ValidaInput;

/**
 * Servlet implementation class ModificaAbilitaAmministratore
 */
public class AggiuntaAbilitaAmministratore extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AggiuntaAbilitaAmministratore() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  		gestisciRichiesta(request, response);
  	}

  	/**
  	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
  	 */
  	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  		gestisciRichiesta(request, response);
  	}
  	
  	private void gestisciRichiesta(HttpServletRequest request, HttpServletResponse response){
  		RequestDispatcher dispatcher = request.getRequestDispatcher("/Amministratore/AggiuntaAbilitaAmministratore.jsp");
		boolean redirect = false;
  		try{
  			Hashtable<String,String> env = new Hashtable<String,String>();
  			env.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
  			env.put(Context.PROVIDER_URL,"localhost:1099");
  			InitialContext jndiContext = new InitialContext(env);
  			Object ref = jndiContext.lookup("ManagerAbilita/remote");
  			ManagerAbilitaRemote manager = (ManagerAbilitaRemote)ref;
  			
  			if(request.getSession().getAttribute("amministratore")!=null){
  				String nomeAbilita,descrizioneAbilita;
				nomeAbilita = request.getParameter("nomeAbilita");
				descrizioneAbilita = request.getParameter("descrizioneAbilita");
				if(nomeAbilita!=null && descrizioneAbilita!=null){
					nomeAbilita = ValidaInput.adeguaNome(nomeAbilita);
					descrizioneAbilita = ValidaInput.adeguaNome(descrizioneAbilita);
				
					if((nomeAbilita!=null && descrizioneAbilita!=null) || (!nomeAbilita.equals("") && !descrizioneAbilita.equals(""))){
						manager.addAbilita(nomeAbilita, descrizioneAbilita);
						dispatcher = request.getRequestDispatcher("/Amministratore/AbilitaAmministratore.jsp");
					}
				}
  			}else{
  				redirect = true;
  				response.sendRedirect("./");
  			}
  			
			
			
  		}catch(Exception e){
  			try {
  				redirect = true;
  				response.sendRedirect("./");
  			} catch (IOException e1) {
  				e1.printStackTrace();
  			}
  		}
  		
  		
  		try {
  			if(!redirect){
  				dispatcher.forward(request, response);
  			}
  		} catch (ServletException e) {
  			e.printStackTrace();
  		} catch (IOException e) {
  			e.printStackTrace();
  		}
  	}

  }